

import xml.etree.ElementTree as ET
if __name__ == '__main__':
    tree=ET.parse("python1.xml")
    newStu=ET.Element("student",attrib={"sex":"人妖"})
    name=ET.SubElement(newStu,"name")
    name.text="魏忠贤"
    age = ET.SubElement(newStu, "age")
    age.text = "9990"
    qq = ET.SubElement(newStu, "qq")
    qq.text = "1333333"
    wechat = ET.SubElement(newStu, "wechat")
    wechat.text = "weidaren"
    phone = ET.SubElement(newStu, "phone")
    phone.text = "1333332122"
    degree = ET.SubElement(newStu, "degree")
    degree.text = "文盲"
    tree.getroot().append(newStu)#追加到xml上
    tree.write('python2.xml', encoding='utf-8', xml_declaration=True)
    print("保存成功")



